﻿Imports System.Data.SqlClient
Imports System.Data.Odbc

Public Class frmCatalogos
    Private ds As DataSet
    Private mstrValor As String
    Private mstrCondicion As String
    Dim mdsDatos As New DataSet
    Dim aux As Integer
    Private mstrValor2 As String
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Public ReadOnly Property Valor2() As String
        Get
            Valor2 = mstrValor2
        End Get
    End Property
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.dgCatalogos.DefaultCellStyle.Font = New Font("Arial", 8, FontStyle.Regular)

        Me.txtBuscar.Clear()
        Me.txtBuscar.Select()
        aux = 0
        mstrValor = ""
        mstrCondicion = ""
        Dim cont As Integer = 0

        Me.dgCatalogos.Rows.Clear()

        Dim cnConn As New ODBCConnection
        cnConn.ConnectionString = AdminPAQConnection1


        Dim cmdBuscar As New OdbcCommand
        cmdBuscar.Connection = cnConn

        '"Select ccodigoc01 as Codigo, cnombrec01 as Nombre  from mgw10006"
        'TextoConsulta2 = "Select ccodigoc01 as Codigo, cnombrec01 as Nombre  from mgw10006 where cnombrec01"

        cmdBuscar.CommandText = "Select ccodigoc01 as Codigo, cnombrec01 as Nombre  from mgw10006"
       


        Dim rdBuscar As OdbcDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Try
            Do While rdBuscar.Read()
                cont = cont + 1
                Me.dgCatalogos.Rows.Add(cont, rdBuscar("Codigo"), rdBuscar("Codigo"), rdBuscar("Nombre"))
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
      

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    Sub actualizar()
        If aux = 0 Then
            Exit Sub

        End If
        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Dim strNombre As String
        Dim cont As Integer = 0

        strNombre = UCase(txtBuscar.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"


        Me.dgCatalogos.Rows.Clear()
        Dim cnConn As New OdbcConnection
        cnConn.ConnectionString = AdminPAQConnection1

        Dim cmdBuscar As New OdbcCommand
        cmdBuscar.Connection = cnConn

        cmdBuscar.CommandText = "SELECT ccodigoc01 as codigo,cnombrec01 from mgw10006 WHERE upper(CCODIGOC01) LIKE '" & strNombre & "'" & mstrCondicion & " OR upper(CNOMBREC01) LIKE '" & strNombre & "' " & mstrCondicion
        Try
            Dim rdBuscar As OdbcDataReader
            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                cont = cont + 1
                Me.dgCatalogos.Rows.Add(cont, rdBuscar("codigo"), rdBuscar("codigo"), rdBuscar("cnombrec01"))
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

       
       
        Windows.Forms.Cursor.Current = Cursors.Default
    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        aux = 1
        actualizar()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim strRetVal As String = ""

        Try
            strRetVal = CStr(dgCatalogos.Item(0, dgCatalogos.CurrentCell.RowIndex).Value)
        Catch ex As Exception
            strRetVal = ""
        Finally
            mstrValor = strRetVal.Trim
        End Try

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
    Private Sub dgResult_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCatalogos.CellDoubleClick

        btnAbrir_Click(Me, System.EventArgs.Empty)

    End Sub

    Private Sub dgResult_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCatalogos.KeyDown

        If e.KeyCode = Keys.Enter Then
            btnAbrir_Click(Me, System.EventArgs.Empty)
        End If

    End Sub

    Private Sub btnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbrir.Click
        Dim strRetVal As String = ""
        Dim strRetVal2 As String = ""
        Try
            strRetVal = CStr(dgCatalogos.Item(3, dgCatalogos.CurrentCell.RowIndex).Value)
            strRetVal2 = CStr(dgCatalogos.Item(2, dgCatalogos.CurrentCell.RowIndex).Value)
        Catch ex As Exception
            strRetVal = ""
        Finally
            mstrValor = strRetVal.Trim
            mstrValor2 = strRetVal2
        End Try

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub dgCatalogos_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCatalogos.CellContentClick

    End Sub
End Class